package com.demo.controller;


import com.common.Dto.CompanyIpoDTO;
import com.common.Dto.CompanyLoginDTO;
import com.common.Dto.CompanyRegisterDTO;
import com.common.VO.CompanyLoginVo;
import com.common.VO.IpoVO;
import com.common.VO.RegisterVO;
import com.common.commont.Messageconstant;
import com.common.result.Result;
import com.demo.interceptor.JwtProperties;
import com.demo.server.impl.CompanyServerImpl;
import com.tools.untils.jwtutils;
import lombok.extern.slf4j.Slf4j;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(("/Company"))
@Slf4j
public class Company {
    @Autowired
    CompanyServerImpl companyServer;
    @Autowired
    JwtProperties jwtProperties;

    /**
     * 公司注册
     * @param companyRegisterDTO
     * @return
     */
    @PostMapping("/register")
    public Result<RegisterVO> company_register(@RequestBody CompanyRegisterDTO companyRegisterDTO){
        log.info("传递的数据:"+companyRegisterDTO);
         return  companyServer.Register(companyRegisterDTO);

    }

    /**
     * 公司登录
     * @param companyLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<CompanyLoginVo>   company_login(@RequestBody CompanyLoginDTO companyLoginDTO){

        if(companyLoginDTO.getAccount() == null || companyLoginDTO.getPassword() == null){
            return Result.error(Messageconstant.ERROR_ACCOUNT_PASSWORD);
        }
        Result<CompanyLoginVo> login = companyServer.Login(companyLoginDTO);
        if(login.getData() == null ){
            return login;
        }
        Map<String,Object> map = new HashMap<>();
        map.put(Messageconstant.user_ID,login.getData().getToken());
        String token = jwtutils.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                map
        );
        login.getData().setToken(token);
        return login;
    }

    /**
     * 公司上市请求
     * @param companyIpoDTO
     * @return
     * @throws ContractException
     */
    @PostMapping("/ipo")
    public Result<IpoVO> company_ipo(@RequestBody CompanyIpoDTO companyIpoDTO) throws ContractException {
        log.info("公司上市请求");
        return companyServer.processIpo(companyIpoDTO);
    }
}
